#include "tamiserversl.h"


//===========ZeroC iceBox export============//
extern "C"
{
TAMISERVERSLSHARED_EXPORT IceBox::Service* create(Ice::CommunicatorPtr)
{
    return new TamiServerSL;
}
}

//========== RobotSession ==========//
class RobotSession : public Robot::Session
{
public:
    RobotSession(ClientSessionManagerI* pClientSM)
    {
        m_pClientSM = pClientSM;
    }
    ~RobotSession()
    {

    }
private:
    ClientSessionManagerI* m_pClientSM;

    // Session interface
public:
    void List(Ice::StringSeq &strList, const Ice::Current &){
        if( m_pClientSM ){
            m_pClientSM->list_session(strList);
        }
    }
};

class RobotLiveI : public Robot::Live{
public:
    RobotLiveI(TMMediaManger* pMedia){
        m_pMedia = pMedia;
    }
private:
    TMMediaManger* m_pMedia;

    // Live interface
public:
    void RemoteRecvStart(const Ice::Current &){
        m_pMedia->StartGuester();
    }

    void RemoteRecvStop(const Ice::Current &){
        m_pMedia->StopGuester();
    }
};

//========== TamiServerSL =========//

TamiServerSL::TamiServerSL()
{
    m_pMediaMgr = new TMMediaManger();
}

void TamiServerSL::start(const std::string &name, const Ice::CommunicatorPtr &comm, const Ice::StringSeq &args)
{
    qDebug()<<"TamiServerSL::start"<<name.c_str();

    // SessionManager
    m_adapter_sm = comm->createObjectAdapter("SessionManagerAD");
    ClientSessionManagerI* csm = new ClientSessionManagerI;
    m_adapter_sm->add(csm, comm->stringToIdentity("SessionManager"));
    m_adapter_sm->activate();

    // Robot::Session
    m_adapter_rs = comm->createObjectAdapter("RobotSL");
    m_adapter_rs->add(new RobotSession(csm), comm->stringToIdentity("Session"));
    RobotLiveI* pLiveI = new RobotLiveI(m_pMediaMgr);
    m_adapter_rs->add(pLiveI, comm->stringToIdentity("Live"));
    m_adapter_rs->activate();

    // RTSP
    m_pMediaMgr->Init();
}

void TamiServerSL::stop()
{
    m_pMediaMgr->Destroy();
    m_adapter_sm->destroy();
    m_adapter_rs->destroy();
}



